package noema.facade;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import noema.bam.InstanceMonitoring;
import noema.dataModel.MyTask;
import noema.dataModel.Patient;
import noema.humanTask.HumanTaskManager;
import noema.processManagement.ProcessManagement;
import noema.processManagement.ghostProcess.GhostProcessManagement;

import org.jbpm.process.audit.NodeInstanceLog;
import org.jbpm.process.audit.VariableInstanceLog;

@WebService
public class WSFacade implements ProcessManagement, HumanTaskManager,
		InstanceMonitoring, GhostProcessManagement {

	/**********************
	 * Process Management *
	 **********************/
	@Override
	@WebMethod
	public Patient startHospitalization(
			@WebParam(name = "healthCardId") String healthCardId,
			@WebParam(name = "patientName") String name,
			@WebParam(name = "patientSurname") String surname) {
		try {
			Context ctx = new InitialContext();
			ProcessManagement pManager = (ProcessManagement) ctx
					.lookup("thesisPrj/ProcessManagementImpl/local");
			return pManager.startHospitalization(healthCardId, name, surname);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	@WebMethod
	public void startPathWay(@WebParam(name = "patient") Patient p,
			@WebParam(name = "pathwayId") String pathwayId) {
		try {
			Context ctx = new InitialContext();
			ProcessManagement pManager = (ProcessManagement) ctx
					.lookup("thesisPrj/ProcessManagementImpl/local");
			pManager.startPathWay(p, pathwayId);
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	@Override
	@WebMethod
	public void abortHospitalization(@WebParam(name = "patient") Patient p) {
		try {
			Context ctx = new InitialContext();
			ProcessManagement pManager = (ProcessManagement) ctx
					.lookup("thesisPrj/ProcessManagementImpl/local");
			pManager.abortHospitalization(p);
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	@Override
	@WebMethod
	public void abortPathway(@WebParam(name = "patient") Patient p,
			@WebParam(name = "pathwayId") String pathwayId) {
		try {
			Context ctx = new InitialContext();
			ProcessManagement pManager = (ProcessManagement) ctx
					.lookup("thesisPrj/ProcessManagementImpl/local");
			pManager.abortPathway(p, pathwayId);
		} catch (NamingException e) {
			e.printStackTrace();
		}

	}
	
	
	/****************
	 * HUMAN TASK *
	 ****************/
	@Override
	@WebMethod
	@WebResult(name = "tasksList")
	public List<MyTask> retrieveTaskList() {
		try {
			Context ctx = new InitialContext();
			HumanTaskManager htManager = (HumanTaskManager) ctx
					.lookup("thesisPrj/HumanTaskManagerImpl/local");
			return htManager.retrieveTaskList();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	@WebMethod
	@WebResult(name = "tasksList")
	public List<MyTask> retrieveTaskListByProcessInstance(
			@WebParam(name = "instanceId") long id) {
		try {
			Context ctx = new InitialContext();
			HumanTaskManager htManager = (HumanTaskManager) ctx
					.lookup("thesisPrj/HumanTaskManagerImpl/local");
			return htManager.retrieveTaskListByProcessInstance(id);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	@WebMethod
	@WebResult(name = "tasksList")
	public List<MyTask> retrieveTaskListByName(
			@WebParam(name = "taskName") String name) {
		try {
			Context ctx = new InitialContext();
			HumanTaskManager htManager = (HumanTaskManager) ctx
					.lookup("thesisPrj/HumanTaskManagerImpl/local");
			return htManager.retrieveTaskListByName(name);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<MyTask> retrieveTaskListByPotentialOwner(String owner) {
		try {
			Context ctx = new InitialContext();
			HumanTaskManager htManager = (HumanTaskManager) ctx
					.lookup("thesisPrj/HumanTaskManagerImpl/local");
			return htManager.retrieveTaskListByPotentialOwner(owner);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	@WebMethod
	public void completeTask(@WebParam(name = "taskId") long id,
			@WebParam(name = "performer") String performer,
			@WebParam(name = "jsonParameter") String jsonParams) {
		try {
			Context ctx = new InitialContext();
			HumanTaskManager htManager = (HumanTaskManager) ctx
					.lookup("thesisPrj/HumanTaskManagerImpl/local");
			htManager.completeTask(id, performer, jsonParams);
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	@Override
	@WebMethod
	public void abortTask(@WebParam(name = "taskId") long id) {
		try {
			Context ctx = new InitialContext();
			HumanTaskManager htManager = (HumanTaskManager) ctx
					.lookup("thesisPrj/HumanTaskManagerImpl/local");
			htManager.abortTask(id);
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	/******************
	 * Ghost Process *
	 ******************/
	@Override
	@WebMethod
	public void forceAction(@WebParam(name = "patient") Patient p,
			@WebParam(name = "pathwayId") String pathway,
			@WebParam(name = "motivation") String why,
			@WebParam(name = "actionDescription") String what,
			@WebParam(name = "actionPerformer") String who) {
		try {
			Context ctx = new InitialContext();
			GhostProcessManagement gpManager = (GhostProcessManagement) ctx
					.lookup("thesisPrj/GhostProcessManagementImpl/local");
			gpManager.forceAction(p, pathway, why, what, who);
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	@Override
	@WebMethod
	public void resumePathway(@WebParam(name = "patient") Patient p,
			@WebParam(name = "pathwayId") String pathway) {
		try {
			Context ctx = new InitialContext();
			GhostProcessManagement gpManager = (GhostProcessManagement) ctx
					.lookup("thesisPrj/GhostProcessManagementImpl/local");
			gpManager.resumePathway(p, pathway);
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	/********************
	 * BAM *
	 ********************/
	@Override
	@WebMethod
	@WebResult(name = "idsOfActiveProcess")
	public List<Long> getIdsOfActiveProcess() {
		try {
			Context ctx = new InitialContext();
			InstanceMonitoring bam = (InstanceMonitoring) ctx
					.lookup("thesisPrj/InstanceMonitoringImpl/local");
			return bam.getIdsOfActiveProcess();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	@WebMethod
	@WebResult(name = "nodeInstance")
	public NodeInstanceLog getActualNodeOfInstance(
			@WebParam(name = "instanceId") long instanceId) {
		try {
			Context ctx = new InitialContext();
			InstanceMonitoring bam = (InstanceMonitoring) ctx
					.lookup("thesisPrj/InstanceMonitoringImpl/local");
			return bam.getActualNodeOfInstance(instanceId);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	@WebMethod
	@WebResult(name = "variableInstance")
	public VariableInstanceLog getVariableOfInstance(
			@WebParam(name = "instanceId") long instanceId,
			@WebParam(name = "variableId") String variableName) {
		try {
			Context ctx = new InitialContext();
			InstanceMonitoring bam = (InstanceMonitoring) ctx
					.lookup("thesisPrj/InstanceMonitoringImpl/local");
			return bam.getVariableOfInstance(instanceId, variableName);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}

}
